﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Host;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProductSearch : BaseModule
    {
        [AccessedThroughProperty("cmdGo")]
        private Button _cmdGo;
        [AccessedThroughProperty("imgGo")]
        private ImageButton _imgGo;
        [AccessedThroughProperty("imgSearch")]
        private Image _imgSearch;
        private string _OrderByQuery;
        private bool _OrderDESCQuery;
        [AccessedThroughProperty("plSearch")]
        private Label _plSearch;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;

        public ProductSearch()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            this.SearchExecute();
        }

        private void imgGo_Click(object sender, ImageClickEventArgs e)
        {
            this.SearchExecute();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            string str = DotNetNuke.Services.Localization.Localization.GetString("imgGo.ImageUrl", this.LocalResourceFile);
            string str3 = DotNetNuke.Services.Localization.Localization.GetString("imgSearch.ImageUrl", this.LocalResourceFile);
            string str4 = Conversions.ToString(this.Settings["txtSImagePath"]);
            string str2 = Conversions.ToString(this.Settings["txtImagePath"]);
            this._OrderByQuery = "";
            if (this.Request.Params["orderby"] != null)
            {
                this._OrderByQuery = this.Request.Params["orderby"].ToString();
            }
            this._OrderDESCQuery = false;
            if (this.Request.Params["desc"] != null)
            {
                this._OrderDESCQuery = true;
            }
            if (str2 == "")
            {
                if (str.StartsWith("~"))
                {
                    this.imgGo.ImageUrl = str;
                }
                else
                {
                    this.imgGo.ImageUrl = Path.Combine(this.PortalSettings.HomeDirectory, str);
                }
            }
            else if (str2.StartsWith("~"))
            {
                this.imgGo.ImageUrl = str2;
            }
            else
            {
                this.imgGo.ImageUrl = Path.Combine(this.PortalSettings.HomeDirectory, str2);
            }
            if (str4 == "")
            {
                if (str3.StartsWith("~"))
                {
                    this.imgSearch.ImageUrl = str3;
                }
                else
                {
                    this.imgSearch.ImageUrl = Path.Combine(this.PortalSettings.HomeDirectory, str3);
                }
            }
            else if (str4.StartsWith("~"))
            {
                this.imgSearch.ImageUrl = str4;
            }
            else
            {
                this.imgSearch.ImageUrl = Path.Combine(this.PortalSettings.HomeDirectory, str4);
            }
            this.ShowHideImages();
            this.cmdGo.Text = DotNetNuke.Services.Localization.Localization.GetString("cmdGo.Text", this.LocalResourceFile);
        }

        private void SearchExecute()
        {
            int tabId;
            if (this.Settings["SearchResultsModule"] != null)
            {
                tabId = int.Parse(Conversions.ToString(this.Settings["SearchResultsModule"]));
            }
            else
            {
                tabId = this.TabId;
            }
            string str = "0";
            if (this._OrderDESCQuery)
            {
                str = "1";
            }
            string[] strArray = this.txtSearch.Text.Split(new char[] { ' ' });
            if (strArray.GetUpperBound(0) >= 0)
            {
                string s = "";
                int upperBound = strArray.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    s = s + this.Server.UrlEncode(strArray[i]) + "+";
                }
                s = s.TrimEnd(new char[] { '+' });
                if (SharedFunctions.GetStoreSettingBoolean(this.PortalId, "createsearchstats.flag"))
                {
                    StatsController controller = new StatsController();
                    int num5 = strArray.GetUpperBound(0);
                    for (int j = 0; j <= num5; j++)
                    {
                        controller.UpdateSearchWordHits(this.PortalId, strArray[j], j + 1);
                    }
                }
                if (HostSettings.GetHostSetting("UseFriendlyUrls") == "Y")
                {
                    if (this._OrderByQuery == "")
                    {
                        this.Response.Redirect(Globals.NavigateURL(tabId) + "?Search=" + this.Server.UrlEncode(s));
                    }
                    else
                    {
                        this.Response.Redirect(Globals.NavigateURL(tabId, "", new string[] { "orderby=" + this._OrderByQuery, "desc=" + str }) + "?Search=" + this.Server.UrlEncode(s));
                    }
                }
                else if (this._OrderByQuery == "")
                {
                    this.Response.Redirect(Globals.NavigateURL(tabId) + "&Search=" + this.Server.UrlEncode(s));
                }
                else
                {
                    this.Response.Redirect(Globals.NavigateURL(tabId, "", new string[] { "orderby=" + this._OrderByQuery, "desc=" + str }) + "&Search=" + this.Server.UrlEncode(s));
                }
            }
        }

        private void ShowHideImages()
        {
            string str = Conversions.ToString(this.Settings["ShowGoImage"]);
            string str2 = Conversions.ToString(this.Settings["ShowSearchImage"]);
            bool flag = false;
            bool flag2 = false;
            if (str != null)
            {
                flag = Conversions.ToBoolean(str);
            }
            if (str2 != null)
            {
                flag2 = Conversions.ToBoolean(str2);
            }
            this.imgSearch.Visible = flag2;
            this.plSearch.Visible = false;
            this.imgGo.Visible = flag;
            this.cmdGo.Visible = !flag;
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            this.SearchExecute();
        }

        protected virtual Button cmdGo
        {
            get
            {
                return this._cmdGo;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdGo_Click);
                if (this._cmdGo != null)
                {
                    this._cmdGo.Click -= handler;
                }
                this._cmdGo = value;
                if (this._cmdGo != null)
                {
                    this._cmdGo.Click += handler;
                }
            }
        }

        protected virtual ImageButton imgGo
        {
            get
            {
                return this._imgGo;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                ImageClickEventHandler handler = new ImageClickEventHandler(this.imgGo_Click);
                if (this._imgGo != null)
                {
                    this._imgGo.Click -= handler;
                }
                this._imgGo = value;
                if (this._imgGo != null)
                {
                    this._imgGo.Click += handler;
                }
            }
        }

        protected virtual Image imgSearch
        {
            get
            {
                return this._imgSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._imgSearch = value;
            }
        }

        protected virtual Label plSearch
        {
            get
            {
                return this._plSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSearch = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.txtSearch_TextChanged);
                if (this._txtSearch != null)
                {
                    this._txtSearch.TextChanged -= handler;
                }
                this._txtSearch = value;
                if (this._txtSearch != null)
                {
                    this._txtSearch.TextChanged += handler;
                }
            }
        }
    }
}

